#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/soundcard.h>
#include <fcntl.h>
#include <sys/time.h>

int Frequency = 48000;
int format = AFMT_S16_LE;
int channels = 1;
int setting = 960;

struct timeval tv;
struct timezone tz;
unsigned char buffer[960+16];//48k 采样, 前面960字节是
int FrameNO = 0;

int main(void)
{
    struct sockaddr_in local_addr, remote_addr;
    int result;
    int socklen;
    int i=0, j=0;
    int fdsocket = socket(AF_INET, SOCK_DGRAM, 0);

    int fdsoundplay = 0;

    fdsoundplay = open("/dev/dsp", O_WRONLY);/*只写方式打开设备*/
    if(fdsoundplay<0)
    {
       perror("以只写方式打开音频设备");
       return -1;
    }
#if 1
    int p = 0x00040009;
    if(ioctl(fdsoundplay, SNDCTL_DSP_SETFRAGMENT, &p) == -1)
    {
        perror("SETFRAGMENT");
    }
#endif
    printf("设置写音频设备参数\n");
    ioctl(fdsoundplay, SNDCTL_DSP_SPEED, &Frequency);//采样频率
    ioctl(fdsoundplay, SNDCTL_DSP_SETFMT, &format);//音频设备位宽
    ioctl(fdsoundplay, SNDCTL_DSP_CHANNELS, &channels);//音频设备通道
    ioctl(fdsoundplay, SNDCTL_DSP_SETFRAGMENT, &setting);//采样缓冲区

    if (fdsocket == -1) 
    {
      perror("socket()");
        return -1;
      //exit(1);
    }

    //printf("bind port = %d, 数据接收开始\n", *((int*)p));
    /* 设置远程连接的信息*/
    local_addr.sin_family = AF_INET;                 /* 注意主机字节顺序*/
    local_addr.sin_port = htons(20000);          /* 远程连接端口, 注意网络字节顺序*/
    local_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* 远程 IP 地址, inet_addr() 会返回网络字节顺序*/
    memset(&(local_addr.sin_zero), 8, 0);                /* 其余结构须置 0*/    
    //i = *((int*)p) - UDP_BIND_PORT_START;
    bind(fdsocket, (struct sockaddr*)&local_addr, sizeof(struct sockaddr));

    #define AM 1000
    #define PI 3.1415926

    //printf("数据接收开始\n");
    while(1)
    {   
#if 0
	    socklen = sizeof(struct sockaddr);
	    result = recvfrom(fdsocket, buffer, sizeof(buffer), 0, (struct sockaddr*)&remote_addr, &socklen);
	    if(result == -1)
	    {
		    perror("数据接收");
	    }
	    else
	    {
#endif
            //printf("FrameNO=%d\n", *((int*)&buffer[960]));
            //usleep(8000);//10ms的音频数据包,8ms内放完不会延迟
            //usleep(9000);//10ms的音频数据包,9ms内放完不会延迟
            //usleep(9500);//10ms的音频数据包,9.5ms内放完不会延迟
            //usleep(9800);//10ms的音频数据包,9.8ms内放完不会延迟
            //usleep(9900);//10ms的音频数据包,9.9ms内放完延迟(有)

            //gettimeofday(&tv, &tz);
            //printf("%ld,", tv.tv_usec/1000);

            j++;
            if (j>30) 
            {
                j=3;
            }

            for(i=0;i<960/sizeof(short);i++)
            {
                ((short*)&buffer[0])[i] = AM * sin(2*PI/j*i);
            }

            
            if(write(fdsoundplay, buffer, 960) < 0)
            {    
                perror("音频设备写错误\n");
            }
            #if 0
            usleep(10000);//延时10ms
            #endif
            gettimeofday(&tv, &tz);
            printf("%3d, FrameNO=%d\n", tv.tv_sec*1000 + tv.tv_usec/1000, FrameNO++);

#if DEBUG_SAVE_RECV_PCM
            fwrite(pWriteHdrRecv[i]->buffer, sizeof(pWriteHdrRecv[i]->buffer), 1, fp);
#endif        

#if 0
	    }
#endif
    }

    return 0;
}
